package com.lailai.constant;

/**
 * http请求结果状态码
 *
 * @Author lgr
 * @Date 2025/6/30
 * @Wechat a983105834
 * @Email 983105834@qq.com
 * @Copyright <a href="www.lailai.ink">lailai个人空间</a>
 */
public enum HttpStatusCode {
    // 1xx - Informational(信息响应)
    CONTINUE(100, "继续。客户端应继续其请求"),
    SWITCHING_PROTOCOLS(101, "切换协议。服务器根据客户端请求切换协议"),

    // 2xx - Success(成功响应)
    SUCCESS(200, "success"),
    CREATED(201, "已创建。成功请求并创建了新资源"),
    ACCEPTED(202, "已接受。已接受请求但未处理完成"),
    NO_CONTENT(204, "无内容。服务器成功处理但未返回内容"),

    // 3xx - Redirection(重定向)
    FOUND(302, "临时移动。资源临时被移动"),
    SEE_OTHER(303, "查看其它地址"),
    TEMPORARY_REDIRECT(307, "临时重定向"),

    // 4xx - Client Error(客户端错误)
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权访问"),
    FORBIDDEN(403, "权限不足"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法被禁止"),
    NOT_ACCEPTABLE(406, "无法根据内容特性完成请求"),
    REQUEST_TIMEOUT(408, "服务器等待请求超时"),
    CONFLICT(409, "服务器处理请求时发生冲突"),
    GONE(410, "资源已永久不存在"),
    LENGTH_REQUIRED(411, "需要Content-Length头信息"),
    UNSUPPORTED_MEDIA(415, "不支持的媒体格式"),
    IM_A_TEAPOT(418, "愚人节玩笑状态码"),

    // 5xx - Server Error(服务器错误)
    ERROR(500, "error"),
    NOT_IMPLEMENTED(501, "服务器不支持请求功能"),
    BAD_GATEWAY(502, "网关从上游服务器收到无效响应"),
    SERVICE_UNAVAILABLE(503, "服务不可用（超载或维护）"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    HTTP_VERSION_NOT_SUPPORTED(505, "不支持的HTTP版本");

    // 状态码
    private final int code;
    // 状态码信息
    private final String message;

    // 构造方法
    HttpStatusCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据code获取对应的HttpStatusCode
     *
     * @param code 状态码
     * @return HttpStatusCode
     */
    public static HttpStatusCode fromCode(int code) {
        for (HttpStatusCode statusCode : values()) {
            if (statusCode.code == code) {
                return statusCode;
            }
        }
        return ERROR; // 默认返回500错误
    }
}
